package polygon.cli.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author MikeMirzayanov (mirzayanovmr@gmail.com)
 */
public class ServeFileUtil {
    public static void update(File file, List<String> keys, List<String> values, String section) throws FileNotFoundException, UnsupportedEncodingException {
        List<String> lines = new ArrayList<String>();

        Scanner scanner = new Scanner(file, "UTF-8");
        while (scanner.hasNextLine()) {
            lines.add(scanner.nextLine());
        }

        int from = -1, to = -1;
        for (int i = 0; i < lines.size() - 1; i++) {
            if (lines.get(i).trim().equals(section)) {
                String found = removeSpaces(lines.get(i + 1));
                String expected = removeSpaces(keys.get(0) + "=" + values.get(0));
                if (found.equals(expected)) {
                    from = i;
                    for (int j = i + 1; j < lines.size(); j++) {
                        if (lines.get(j).trim().startsWith("[")) {
                            break;
                        } else {
                            to = j;
                        }
                    }
                }
            }
        }

        boolean previous = false;

        if (from == -1) {
            for (int i = 0; i < lines.size(); i++) {
                if (lines.get(i).trim().startsWith("[")) {
                    if (lines.get(i).trim().equals(section)) {
                        previous = true;
                    } else {
                        if (previous) {
                            previous = false;
                            from = i;
                            to = i - 1;
                        }
                    }
                }
            }
        }

        if (from == -1) {
            from = lines.size();
            to = lines.size() - 1;
        }

        PrintWriter writer = new PrintWriter(file, "UTF-8");

        for (int i = 0; i <= lines.size(); i++) {
            if (i == from) {
                writer.println(section);
                for (int j = 0; j < keys.size(); j++) {
                    writer.println(keys.get(j) + " = " + values.get(j));
                }
                if (i < lines.size()) {
                    writer.println();
                }
            }

            if (i < from || i > to) {
                if (i < lines.size()) {
                    writer.println(lines.get(i));
                }
            }
        }

        writer.close();
    }

    private static String removeSpaces(String line) {
        String[] tokens = line.split("[ \\t\\n\\r\\f]");
        StringBuilder sb = new StringBuilder();
        for (String token : tokens) {
            if (!token.isEmpty()) {
                sb.append(token);
            }
        }
        return sb.toString();
    }
}
